#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'xdao07'

from salt import SaltStack

"""
sh# bash abc-backend-java.simple.sh abc-backend-java test 2020-1110-1235-98KD javademo-0.0.1-SNAPSHOT.jar 28080 status tot http://192.168.110.111 javademo-1.0.tar.gz
# a(app) e(env) v(version) z(zip) p(pkg) o(port) c(act) i(inc_tot) u(url)
sh# bash abc-backend-java.sh -a abc-backend-java -e test -v 2020-1110-1235-98KD -z javademo-1.0.tar.gz -p javademo-0.0.1-SNAPSHOT.jar -o 28080 -c stop -i TOT -u http://192.168.110.111
"""
def demo():
    sapi = SaltStack(host="192.168.110.111",
                     port="8899",
                     username="salt-api-client",
                     password="salapiclient",
                     secure=True)
    # 为了语义明晰，使用列表
    attach_arg_list = [None] * 9
    attach_arg_list[0] = "-a abc-backend-java"
    attach_arg_list[1] = "-e test"
    attach_arg_list[2] = "-v 2020-1110-1235-98KD"
    attach_arg_list[3] = "-p javademo-0.0.1-SNAPSHOT.jar"
    attach_arg_list[4] = "-o 28080"
    attach_arg_list[5] = "-c start"
    attach_arg_list[6] = "-i tot"
    attach_arg_list[7] = "-u http://192.168.110.111"
    attach_arg_list[8] = "-z javademo-1.0.tar.gz"

    # cmd_script 后面附加参数为字符串，所以要进行转换
    attach_arg = ' '.join(attach_arg_list)
    result = sapi.cmd_script(tgt="192.168.110.201", arg=["http://192.168.110.111/scripts/abc-backend-java.sh", attach_arg])
    print(result['return'][0]['192.168.110.201']['stdout'])


if __name__ == '__main__':
    demo()
